Code notes below for teaching multivariable calculcus.
Let \(f: {\rm I\!R^2 \rightarrow\rm I\!R}\) be differentiable at \(\vec{x}_0=(x_0, y_0)\). The plane in \(\rm I\!R^3\) defined by the equation:
\[ z = f(x_0, y_0) + \left [ \frac{\partial f}{\partial x} (x, y_0)\right ](x - x_0) + \left [ \frac{\partial f}{\partial y}(x_0, y_0) \right ](y - y_0) \]
is called the tangent plane of the graph \(f\) at the point \((x_0, y_0)\).
library(plotly)
library(pracma)
library(Deriv)
plot_tangent_surface = function(f, x_0, y_0, grid_x = -10, grid_y = 10) {
# Create meshgrid for plotting
m = meshgrid(grid_x:grid_y)
nrows = nrow(m$X)
X = as.vector(m$X)
Y = as.vector(m$Y)
# z point for surface.
z_0 = f(x_0, y_0)
# Compute partial derivatives.
df_dx = Deriv(f, 'x')
df_dy = Deriv(f, 'y')
# Create F surface
F_MAT = f(X, Y)
# Create tangent surface
SURFACE = z_0 + df_dx(x_0, y_0)*(X - x_0) + df_dy(x_0, y_0)*(Y - y_0)
# Convert back to matrix for plotly.
F_MAT = matrix(F_MAT, nrow = nrows)
SURFACE = matrix(SURFACE, nrow = nrows)
p <- plot_ly(z = ~F_MAT) %>% add_surface(
contours = list(
z = list(
show=TRUE,
usecolormap=TRUE,
highlightcolor="#ff0000",
project=list(z=TRUE)
)
)
) %>%
layout(
scene = list(
camera=list(
eye = list(x=1.87, y=0.88, z=-0.64)
)
)
) %>%
add_surface(z = ~SURFACE)
p
}# Define f
f = function(x, y) {
x^2 + y^2
}
# Points of intercept with F.
x_0 = -3
y_0 = -3
plot_tangent_surface(f, x_0, y_0, -15, 15)# Define f
f = function(x, y) {
x^(1/3) + y^(1/3)
}
# Points of intercept with F.
x_0 = 1
y_0 = 1
plot_tangent_surface(f, x_0, y_0, -2, 5)